package com.zfj.servlet;

import com.zfj.entity.Admin;
import com.zfj.service.AdminService;
import com.zfj.service.AdminServiceImpl;
import lombok.extern.slf4j.Slf4j;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

/**
 * @author zfj
 * @date 2022/6/27 -20:44
 */
@Slf4j
@WebServlet(value = "/login")
public class LoginServlet extends HttpServlet {
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//		super.doGet(req, resp);
		req.setCharacterEncoding("utf-8");
		resp.setContentType("text/html;charset=utf-8");
		//收参
		String username = req.getParameter("username");
		String password = req.getParameter("password");
		log.info("username={},password={}",username,password);
		//调用业务逻辑
		AdminService adminService=new AdminServiceImpl();
		Admin admin = adminService.login(username, password);
		//处理结果
		PrintWriter writer = resp.getWriter();
		if (admin!=null){
			//相应给客户端一个结果页面，显示登录成功
			writer.println("<html>");
			writer.println("<head>");
			writer.println("<meta charset='utf-8'/>");
			writer.println("<title>结果页面<title/>");
			writer.println("</head>");
			writer.println("<body>");
			writer.println("<h1>登录成功！</h1>");
			writer.println("</body>");
			writer.println("</html>");
		}else {
			//相应给客户端一个结果页面，显示登录失败
			writer.println("<html>");
			writer.println("<head>");
			writer.println("<meta charset='utf-8'/>");
			writer.println("<title>结果页面<title/>");
			writer.println("</head>");
			writer.println("<body>");
			writer.println("<h1>登录失败！</h1>");
			writer.println("</body>");
			writer.println("</html>");

		}
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doGet(req,resp);
	}
}
